﻿#include "ThostFtdcMdSpiImpl.h"
#include "ctpdatastorage.h"
#include"cctprequestmode.h"
#include <iostream>
#include <QDebug>

CThostFtdcMdSpiImpl::CThostFtdcMdSpiImpl(void)
{
	//参数初始化
	m_iRequestId = 0;
    m_bRspSubMarketDataFlg = false;
}

CThostFtdcMdSpiImpl::~CThostFtdcMdSpiImpl(void)
{
}

int CThostFtdcMdSpiImpl::GetRequestId()//获取请求函数的ID号
{
	m_iRequestId ++;
	return m_iRequestId;
}

void CThostFtdcMdSpiImpl::SetUserApi(CThostFtdcMdApi *pUserApi){
    m_pUserApi = pUserApi;
}

void  CThostFtdcMdSpiImpl::SetUserAndPassword(QString sBrokerid ,QString sUser,QString sPassword)
{
       m_accountid=sUser;
       m_password=sPassword;
       m_brokerid=sBrokerid;
}
void CThostFtdcMdSpiImpl::SetCTPRequestMode(void *oCTPRequestMode){
    m_oCTPRequestMode = oCTPRequestMode;
}

///当客户端与交易后台建立起通信连接时（还未登录前），该方法被调用。
void CThostFtdcMdSpiImpl::OnFrontConnected()
{
	QString time = QTime::currentTime().toString("hh:mm:ss");
    qDebug()<<time<<" Md OnFrontConnected";
    CComFun::OutputMessage(time + "Market OnFrontConnected");

    CThostFtdcReqUserLoginField logInfo;
    memset(&logInfo, 0, sizeof(CThostFtdcReqUserLoginField));
    strncpy(logInfo.BrokerID, m_brokerid.toStdString().c_str(), sizeof(TThostFtdcBrokerIDType));
    strncpy(logInfo.UserID,   m_accountid.toStdString().c_str(), sizeof(TThostFtdcUserIDType));
    strncpy(logInfo.Password, m_password.toStdString().c_str(), sizeof(TThostFtdcPasswordType));

    int iMdRet = m_pUserApi->ReqUserLogin(&logInfo, this->GetRequestId());
}

///当客户端与交易后台通信连接断开时，该方法被调用。当发生这个情况后，API会自动重新连接，客户端可不做处理。
///@param nReason 错误原因    
///        0x1001 网络读失败  
///        0x1002 网络写失败  
///        0x2001 接收心跳超时
///        0x2002 发送心跳失败
///        0x2003 收到错误报文
void CThostFtdcMdSpiImpl::OnFrontDisconnected(int nReason)
{
    QString time = QTime::currentTime().toString("hh:mm:ss");
    QString msg = time;

	switch(nReason){
    case 0x1001:
        msg += " Md OnFrontDisconnected:Network Read Failure";
		break;
    case 0x1002:
        msg += " Md OnFrontDisconnected:Network Write Failure";
		break;
    case 0x2001:
        msg += " Md OnFrontDisconnected:Read HeartBeat Timeout";
		break;
    case 0x2002:
        msg += " Md OnFrontDisconnected:Send HeartBeat Timeout";
		break;
    case 0x2003:
        msg += " Md OnFrontDisconnected:Received unknown Error Message";
		break;
	}
    CComFun::OutputMessage(msg);
}
///心跳超时警告。当长时间未收到报文时，该方法被调用。
///@param nTimeLapse 距离上次接收报文的时间
void CThostFtdcMdSpiImpl::OnHeartBeatWarning(int nTimeLapse)
{

}
///登录请求响应
void CThostFtdcMdSpiImpl::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
	QString time = QTime::currentTime().toString("hh:mm:ss");
    if(pRspInfo->ErrorID == 0) {
        qDebug()<<time<<" "<<pRspInfo->ErrorID<<" "<< QString::fromLocal8Bit(pRspInfo->ErrorMsg) <<" Md Login Success";
    } else {
        qDebug()<<time<<" "<<pRspInfo->ErrorID<<" "<< QString::fromLocal8Bit(pRspInfo->ErrorMsg) <<" Md Login Failure";
    }
}
///登出请求响应
void CThostFtdcMdSpiImpl::OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
}
///错误应答
void CThostFtdcMdSpiImpl::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
	qDebug("Md err occured");
    //((MainWindow*)m_lpMainDlg)->OnMdErrOccur(pRspInfo->ErrorID);
}
///订阅行情应答
void CThostFtdcMdSpiImpl::OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
    if(0 == pRspInfo->ErrorID && m_bRspSubMarketDataFlg == false){
        ((CCTPRequestMode*)m_oCTPRequestMode)->StartSaveMarketInfoThrd();
        m_bRspSubMarketDataFlg = true;
        qDebug("Md subscribe Rsp Received Success");
    }
}
///取消订阅行情应答
void CThostFtdcMdSpiImpl::OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
}
///订阅询价应答
void CThostFtdcMdSpiImpl::OnRspSubForQuoteRsp(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
}
///取消订阅询价应答
void CThostFtdcMdSpiImpl::OnRspUnSubForQuoteRsp(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{

}
///深度行情通知
void CThostFtdcMdSpiImpl::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)
{
    ((CCTPRequestMode*)m_oCTPRequestMode)->OnRtnDepthMarketData(pDepthMarketData);

    /*QStringList szHqList;

    ///交易日
    szHqList << QString(pDepthMarketData->TradingDay);
    ///合约代码
    szHqList << QString(pDepthMarketData->InstrumentID);
    ///交易所代码
    szHqList << QString(pDepthMarketData->ExchangeID);
    ///合约在交易所的代码
    szHqList << QString(pDepthMarketData->ExchangeInstID);
    ///最新价
    szHqList << QString::number(pDepthMarketData->LastPrice);
    ///上次结算价
    szHqList << QString::number(pDepthMarketData->PreSettlementPrice);
    ///昨收盘
    szHqList << QString::number(pDepthMarketData->PreClosePrice);
    ///昨持仓量
    szHqList << QString::number(pDepthMarketData->PreOpenInterest);
    ///今开盘
    szHqList << QString::number(pDepthMarketData->OpenPrice);
    ///最高价
    szHqList << QString::number(pDepthMarketData->HighestPrice);
    ///最低价
    szHqList << QString::number(pDepthMarketData->LowestPrice);
    ///数量
    szHqList << QString::number(pDepthMarketData->Volume);
    ///成交金额
    szHqList << QString::number(pDepthMarketData->Turnover);
    ///持仓量
    szHqList << QString::number(pDepthMarketData->OpenInterest);
    ///今收盘
    szHqList << QString::number(pDepthMarketData->ClosePrice);
    ///本次结算价
    szHqList << QString::number(pDepthMarketData->SettlementPrice);
    ///涨停板价
    szHqList << QString::number(pDepthMarketData->UpperLimitPrice);
    ///跌停板价
    szHqList << QString::number(pDepthMarketData->LowerLimitPrice);
    ///昨虚实度
    szHqList << QString::number(pDepthMarketData->PreDelta);
    ///今虚实度
    szHqList << QString::number(pDepthMarketData->CurrDelta);
    ///最后修改时间
    szHqList << QString(pDepthMarketData->UpdateTime);
    ///最后修改毫秒
    szHqList << QString::number(pDepthMarketData->UpdateMillisec);
    ///申买价一
    szHqList << QString::number(pDepthMarketData->BidPrice1);
    ///申买量一
    szHqList << QString::number(pDepthMarketData->BidVolume1);
    ///申卖价一
    szHqList << QString::number(pDepthMarketData->AskPrice1);
    ///申卖量一
    szHqList << QString::number(pDepthMarketData->AskVolume1);
    ///申买价二
    szHqList << QString::number(pDepthMarketData->BidPrice2);
    ///申买量二
    szHqList << QString::number(pDepthMarketData->BidVolume2);
    ///申卖价二
    szHqList << QString::number(pDepthMarketData->AskPrice2);
    ///申卖量二
    szHqList << QString::number(pDepthMarketData->AskVolume2);
    ///申买价三
    szHqList << QString::number(pDepthMarketData->BidPrice3);
    ///申买量三
    szHqList << QString::number(pDepthMarketData->BidVolume3);
    ///申卖价三
    szHqList << QString::number(pDepthMarketData->AskPrice3);
    ///申卖量三
    szHqList << QString::number(pDepthMarketData->AskVolume3);
    ///申买价四
    szHqList << QString::number(pDepthMarketData->BidPrice4);
    ///申买量四
    szHqList << QString::number(pDepthMarketData->BidVolume4);
    ///申卖价四
    szHqList << QString::number(pDepthMarketData->AskPrice4);
    ///申卖量四
    szHqList << QString::number(pDepthMarketData->AskVolume4);
    ///申买价五
    szHqList << QString::number(pDepthMarketData->BidPrice5);
    ///申买量五
    szHqList << QString::number(pDepthMarketData->BidVolume5);
    ///申卖价五
    szHqList << QString::number(pDepthMarketData->AskPrice5);
    ///申卖量五
    szHqList << QString::number(pDepthMarketData->AskVolume5);
    ///当日均价
    szHqList << QString::number(pDepthMarketData->AveragePrice);
    ///业务日期
    szHqList << QString(pDepthMarketData->ActionDay);

    int nIndex = CComFun::RandomNum(WORK_QUEUE - 1);
    CTPDataStorage::SetData(szHqList, nIndex);*/
}
///询价通知
void CThostFtdcMdSpiImpl::OnRtnForQuoteRsp(CThostFtdcForQuoteRspField *pForQuoteRsp)
{
}
